package edu.pitt.dbmi.odie.gapp.gwt.client;

/*
 * SmartGWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * SmartGWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  SmartGWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.google.gwt.event.shared.HandlerRegistration;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;

import edu.pitt.dbmi.odie.gapp.gwt.client.analysis.ODIE_AnalysesManagementPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.analysis.ODIE_AnalysisCreationPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.dao.ODIE_DaoAnalysis;
import edu.pitt.dbmi.odie.gapp.gwt.client.filter.ODIE_FilterPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.foundry.ODIE_FoundryPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.navigator.ODIE_NavigatorPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.umls.ODIE_UmlsPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.user.ODIE_LoginPanel;

public class ODIE_TabbedNavigator extends HLayout {

	public static ODIE_TabbedNavigator self;

	private final TabSet topTabSet = new TabSet();

	private Tab selectedTab = null;

	private ODIE_LoginPanel loginPanel = null;
	private ODIE_AnalysisCreationPanel analysisCreationPanel = null;
	private ODIE_AnalysesManagementPanel analysesManagementPanel = null;
	private ODIE_NavigatorPanel navigatorPanel = null;
	private ODIE_FoundryPanel foundryPanel = null;
	private ODIE_UmlsPanel umlsPanel = null;
	private ODIE_FilterPanel filterPanel = null ;
	
	private ODIE_DaoAnalysis currentAnalysis = null ;

	private HandlerRegistration handlerRegistration ;
	
	private TabSelectedHandler tabSelectedHandler = null;



	public ODIE_TabbedNavigator() {

		this.tabSelectedHandler = new TabSelectedHandler() {
			public void onTabSelected(TabSelectedEvent event) {
				selectTab(event);
			}
		};

		this.handlerRegistration = this.topTabSet.addTabSelectedHandler(this.tabSelectedHandler);

		topTabSet.setTabBarPosition(Side.TOP);
		topTabSet.setWidth("95%");
		topTabSet.setHeight("100%");

		int currentTabPosition = 0;

		String tabID = "User";
		Tab tab = new Tab(tabID);
		tab.setID(tabID);
		loginPanel = new ODIE_LoginPanel(this);
		tab.setPane(loginPanel);
		topTabSet.addTab(tab, currentTabPosition++);

		tabID = "Analysis";
		tab = new Tab(tabID);
		tab.setID(tabID);
		this.analysisCreationPanel = new ODIE_AnalysisCreationPanel();
		tab.setPane(this.analysisCreationPanel);
		topTabSet.addTab(tab, currentTabPosition++);

		tabID = "Analyses";
		tab = new Tab(tabID);
		tab.setID(tabID);
		this.analysesManagementPanel = new ODIE_AnalysesManagementPanel();
		tab.setPane(this.analysesManagementPanel);
		topTabSet.addTab(tab, currentTabPosition++);

		tabID = "Navigator";
		tab = new Tab(tabID);
		tab.setID(tabID);
		this.navigatorPanel = new ODIE_NavigatorPanel();
		tab.setPane(this.navigatorPanel);
		topTabSet.addTab(tab, currentTabPosition++);

		tabID = "Foundry";
		tab = new Tab(tabID);
		tab.setID(tabID);
		this.foundryPanel = new ODIE_FoundryPanel();
		tab.setPane(this.foundryPanel);
		topTabSet.addTab(tab, currentTabPosition++);

//		tabID = "Umls";
//		tab = new Tab(tabID);
//		tab.setID(tabID);
//		this.umlsPanel = new ODIE_UmlsPanel();
//		tab.setPane(this.umlsPanel);
//		topTabSet.addTab(tab, currentTabPosition++);
		
		tabID = "Filter";
		tab = new Tab(tabID);
		tab.setID(tabID);
		this.filterPanel = new ODIE_FilterPanel();
		tab.setPane(this.filterPanel);
		topTabSet.addTab(tab, currentTabPosition++);

		setMembersMargin(1);
		addMember(topTabSet);
		setWidth100();
		setHeight("100%");

		self = this;

	}
	
	private void selectTab(TabSelectedEvent event) {
		this.selectedTab = event.getTab();
		String id = this.selectedTab.getID();
		if (id.equals("Analyses")) {
			this.analysesManagementPanel.refresh();
		} else if (id.equals("Navigator")) {
			this.navigatorPanel.refresh() ;
		} else if (id.equals("Filter")) {
			this.filterPanel.refresh() ;
		}
	}


	public void selectTab(String tabID) {
//		this.topTabSet.redraw() ;
//		this.handlerRegistration.removeHandler() ;
		this.topTabSet.selectTab(getPositionOfTab(tabID)) ;
//		this.handlerRegistration = this.topTabSet.addTabSelectedHandler(this.tabSelectedHandler);
	}

	private int getPositionOfTab(String tabID) {
		int result = -1;
		Tab[] tabs = this.topTabSet.getTabs();
		for (int tdx = 0; tdx < tabs.length; tdx++) {
			if (tabs[tdx].getID().equals(tabID)) {
				result = tdx;
				break;
			}
		}
		return result;
	}

	public void refreshAnalysesManagementTab() {
		Tab tab = this.topTabSet.getTab("Analyses") ;
		this.analysesManagementPanel = new ODIE_AnalysesManagementPanel();
        tab.setPane(this.analysesManagementPanel) ;
	}
	
	
	public void refreshNavigatorTab() {
		this.navigatorPanel = new ODIE_NavigatorPanel();
        Tab tab = this.topTabSet.getTab("Navigator") ;
        tab.setPane(this.navigatorPanel) ;
	}
	
	public ODIE_DaoAnalysis getCurrentAnalysis() {
		return currentAnalysis;
	}


	public void setCurrentAnalysis(ODIE_DaoAnalysis currentAnalysis) {
		this.currentAnalysis = currentAnalysis;
	}

}
